package kr.saeildamil.androidforstudy2.ch21;

import kr.saeildamil.androidforstudy2.R;
import android.content.Context;
import android.view.ActionProvider;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class CounterProvider extends ActionProvider {
	Context mContext;
	TextView mCounter;
	public CounterProvider(Context context) {
		super(context);
		this.mContext = context;
	}

	@Override
	public View onCreateActionView() {
		LayoutInflater inflater = LayoutInflater.from(mContext);
		View linear = inflater.inflate(R.layout.ch21_counterprovidert, null);
		mCounter = (TextView) linear.findViewById(R.id.ch21_counterprovider_count);
		
		linear.findViewById(R.id.ch21_counterprovider_addbtn).setOnClickListener(listener);
		linear.findViewById(R.id.ch21_counterprovider_subbtn).setOnClickListener(listener);
		return linear;
	}
	
	OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			int count = Integer.parseInt(mCounter.getText().toString());
			switch (v.getId()) {
			case R.id.ch21_counterprovider_addbtn:
				mCounter.setText(Integer.toString(count+1));
				break;
			case R.id.ch21_counterprovider_subbtn:
				mCounter.setText(Integer.toString(count-1));
				break;
			}
		}
	};
	
	public boolean onPerformDefaultAction() {
		Toast.makeText(mContext, "Counter Menu Item Selected : " + "onPerformDefaultAction", 0);
		return true;
	};
}
